package com.zjitc.controller;

import com.zjitc.Model.User;
import com.zjitc.service.UserService;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Kochiya on 2018/3/20.
 */
@Controller
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/findAllUser")
  public ModelAndView findAll() {
    List<User> listUsers = userService.findAll();

    ModelAndView mv = new ModelAndView();
    mv.addObject("users", listUsers);
    mv.setViewName("/WEB-INF/views/users.jsp");

    return mv;
  }

  @RequestMapping("/findDel")
  public ModelAndView findDel(HttpServletRequest request) {
    String code = request.getParameter("code");
    List<User> Userlist = userService.findDel(code);
//        ModelAndView mv = new ModelAndView("/WEB-INF/views/users.jsp");
    ModelAndView mv = new ModelAndView("redirect:/Kochiya/findAllUser");
//        mv.setViewName("/WEB-INF/views/users.jsp");
    return mv;

  }

  @RequestMapping("/findUI")
  public ModelAndView findUI() {
    ModelAndView mv = new ModelAndView();
    mv.setViewName("/WEB-INF/views/add.jsp");
    return mv;
  }

  @RequestMapping("/findAdd")
  public ModelAndView findAdd(HttpServletRequest request) {
    User user = new User();
    try {
      BeanUtils.populate(user, request.getParameterMap());
     int abc=userService.findAdd(user);
    } catch (Exception e) {
      e.printStackTrace();
    }
    ModelAndView mv = new ModelAndView("redirect:/Kochiya/findAllUser");
    return mv;
  }

  @RequestMapping("/findByid")
  public ModelAndView findByid(HttpServletRequest request) {
    String code = request.getParameter("code");
    User listUsers = userService.findByid(code);
    ModelAndView mv = new ModelAndView();
    mv.addObject("users", listUsers);
    mv.setViewName("/WEB-INF/views/update.jsp");
    return mv;
  }

  @RequestMapping("/findUpdate")
  public ModelAndView findUpdate(HttpServletRequest request) {
    User user = new User();
    try {
      BeanUtils.populate(user, request.getParameterMap());
      userService.findUpdate(user);
    } catch (Exception e) {
      e.printStackTrace();
    }
    ModelAndView mv = new ModelAndView("redirect:/Kochiya/findAllUser");
    return mv;
  }

  @RequestMapping("/findByAll")
  public ModelAndView findByAll(HttpServletRequest request) {
    String region = request.getParameter("Byall");
    List<User> listUsers = userService.findByAll(region);
    ModelAndView mv = new ModelAndView();
    mv.addObject("users", listUsers);
    mv.addObject("Byall", region);
    mv.setViewName("/WEB-INF/views/users.jsp");
    return mv;
  }
}
